<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path;
%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=0">
<title>添加辅导课程-云校通</title>
<jsp:include page="/WEB-INF/page/common/_header.jsp"></jsp:include>
<script src="<%=basePath %>/js/picker.js"></script>
</head>
<body ontouchstart>
	<div class="weui_tab tab-bottom">
		<div class="weui_tab_bd">
			
			<div class="weui-header bg-blue"> 
				<div class="weui-header-left"> <a href="<%=basePath %>/ls/course/coach" class="icon icon-109 f-white">返回</a></div>
				<h1 class="weui-header-title">添加课程辅导</h1>
			</div>
			<form id="coachForm">
		        <!--添加辅导记录-->
	            <div class="weui_cells" style="margin: 0;">
	            	<c:choose>
	            		<c:when test="${coach != null }">
	            			<input type="hidden" id="id" name="id" value="${coach.coachId }"/>
	            			<input type="hidden" id="coachType" name="coachType" value="1"/>
	            			<div class="weui_cell">
								<div class="weui_cell_bd weui_cell_primary">
									<p>辅导学生</p>
								</div>
								<div class="weui_cell_ft f-black"> <input class="weui_input tright" type="text" name="studentName" id="studentName" value="${coach.studentName }" readonly/></div>
							</div>
							<div class="weui_cell">
								<div class="weui_cell_bd weui_cell_primary">
									<p>请假时间</p>
								</div>
								<div class="weui_cell_ft f-black"><fmt:formatDate value="${coach.leaveStartTime }" pattern="MM/dd HH:mm"/> - <fmt:formatDate value="${coach.leaveEndTime }" pattern="MM/dd HH:mm"/></div>
							</div>
	            		</c:when>
	            		<c:otherwise>
	            			<input type="hidden" id="coachType" name="coachType" value="2"/>
	            			<input type="hidden" id="coachStatus" name="coachStatus" value="000"/>
	            			<input type="hidden" id="studentIds" name="studentIds"/>
							<div class="searchbar_wrap"></div>
							<div id="search-content" style="height: 100%;z-index: 10;display:none;">
						       <div class="sb-content" style="position:inherit;overflow:scroll;">
						       </div>
						    </div>
						    <div>
						    	<ul id="chooseStudent">
						    	</ul>
						    </div>
	            		</c:otherwise>
	            	</c:choose>
				</div>
	            <div class="weui_cells borderLeft" style="margin-top:1px;">
					<div class="weui_cell">
						<div class="weui_cell_bd weui_cell_primary">
							<p>辅导课程</p>
						</div>
						<div class="weui_cell_ft f-black"><input class="weui_input tright" type="text" name="subject" id=subject value="${user.teacher.subjectText }" readonly/></div>
					</div>
					<div class="weui_cell">
						<div class="weui_cell_bd weui_cell_primary">
							<p>辅导老师</p>
						</div>
						<div class="weui_cell_ft f-black">${user.teacher.name }</div>
					</div>
					<div class="weui_cell">
						<div class="weui_cell_bd weui_cell_primary">
							<p>辅导时间</p>
						</div>
						<div class="weui_cell_ft f-black"><input class="weui_input tright" type="text" name="coachDate" id="coachDate" value="" placeholder="请选择" readonly=""></div>
					</div>
					
					<div class="weui_cell tjjsqj-line-top">
						<div class="weui_cell_hd">
							<label class="weui_label">辅导情况</label>
						</div>
						<div class="weui_cell_primary">
		                    <textarea id="coachResult" name="coachResult" class="weui_textarea" placeholder="请输入辅导内容、辅导方法、辅导效果等。" rows="6"></textarea>
						</div>
					</div>
		        </div>
				<div class="weui_btn_area">
			        <a id="formSubmitBtn" href="javascript:" class="weui_btn btn-lg bg-blue">确认提交</a>
			    </div> 
	            <!--End添加辅导记录-->
            </form>
		</div>
	</div>
<jsp:include page="/WEB-INF/page/common/_footer.jsp"></jsp:include>
<script>
	$(function() {
		var nowDate = new Date();
		var min = nowDate.getFullYear() + "-" + ("00"+(nowDate.getMonth())).substr(-2);
		$("#coachDate").datetimePicker({title:"选择日期时间",min:min});
		$('.searchbar_wrap').searchBar({
			cancelText: "取消",
			searchText: '请输入辅导学生',
			onfocus: function(value) {
				$("#search-content").show();
				$(".weui_panel").hide();
			},
			onblur: function(value) {
			},
			input: function(value) {
				queryStudentItem(value);
			},
			onsubmit: function(value) {
				queryStudentItem(value);
			},
			oncancel: function() {
				$("#search-content").hide();
				$(".sb-content").html('');
			},
			onclear: function() {
				$("#search-content").hide();
				$(".sb-content").html('');
				$("#studentName").val(null);
				$("#studentId").val(null);
			}
		});
		$(document).on("click", "#chooseStudent li", function(e) {
			var studentId = $(this).attr("data-id");
			$(this).remove();
			var studentIds = $("#studentIds").val();
			var array = studentIds.split(",");
			var index = array.indexOf(studentId);
			if (index > -1) {
				array.splice(index, 1);
			}
			$("#studentIds").val(array.toString());
		});
		$("#formSubmitBtn").click(function() {
			var coachType = $("#coachType").val();
			if(coachType == '2') {
				if($("#studentIds").val().trim() == "") {
					$.toast("请选择学生", "cancel");
					return;
				}
			}
			if($("#coachDate").val() == "") {
				$.toast("请选择辅导时间", "cancel");
				return;
			}
			if($("#coachResult").val() == "") {
				$.toast("请输入辅导情况", "cancel");
				return;
			}
			$.ajax({
		 		url : "<%=basePath%>/ls/course/coach/addUpdate",
		 		type : "post",
		 		data : $('#coachForm').serialize(),
		 		dataType : "json",
		 		async : false,
		 		success : function(data) {
		 			if(data.code == '000') {
		 				$.toast("添加辅导成功!");
		 				setTimeout(function(){
		 					window.location.href=basePath + "/ls/course/coach";
		 				},1000);
		 			}else {
		 				$.toast(data.msg, "cancel");
		 			}
		 		}, error:function(){
		 			$.toast("系统异常，请联系管理员",'forbidden');
	    		}
		 	});
		})
	});
	var search = false;
	function queryStudentItem(keyword) {
		if(keyword != null) {
			if(!/^[\u4E00-\u9FA5]+$/.test(keyword)) return;
			if(search) return;
			search = true;
		}
		$.ajax({
    		url: basePath + "/ls/students/manage/keyword",
    		type: 'POST',
    		dataType : "json",
    		data: {
    			grade: "${model.grade}",
    			clazz: "${model.clazz}",
    			studentName: keyword
    		},
    		success: function(data) {
    			search = false;
    			var html = "";
    			if (data != null && data.length > 0) {
    				for(var i in data) {
    					var itm = data[i];
    					html += '<div class="weui_cell"><p onclick=choseStudent('+itm.id+',"'+itm.name+'")>'+ itm.name + ' ' + itm.gradeName + '（' + itm.clazz + '班）</p></div>';
    				}
    			}
    			$(".sb-content").html(html);
    		},
    		error: function(data) {
    			alert("系统异常,请联系管理员");
    		}
    	})
	}
	function choseStudent(studentId, studentName) {
		$("#weui_search_input").val(null);
		$(".sb-content").html('');
		$("#search-content").hide();
		var studentIds = $("#studentIds").val();
		if((studentIds + ",").indexOf(studentId + ",") == -1) {
			if(studentIds.length > 0) {
				studentIds += ",";
			}
			studentIds += studentId;
			$("#studentIds").val(studentIds);
			$("#chooseStudent").append('<li onclick="listDel(this)" class="list-del" data-id="'+ studentId +'">'+ studentName +'<span class="list-del-vaild"></span></li>');
		}
	}
	function listDel(thiz){
   		$(thiz).remove();
   	}
</script>
</body>
</html>